package com.open.lua;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.keplerproject.luajava.LuaState;
import org.keplerproject.luajava.LuaStateFactory;

import com.example.androidluatest.R;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	private LuaState mLuaState;// Lua解析和执行由此对象完成

	private TextView displayResult1;// 用于演示，显示数据
	private TextView displayResult2;
	private LinearLayout mLayout;

	private static int count;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		displayResult1 = (TextView) findViewById(R.id.displayResult1);
		displayResult2 = (TextView) findViewById(R.id.displayResult2);
		mLayout = (LinearLayout) findViewById(R.id.layout);

		findViewById(R.id.executeLuaStatemanet).setOnClickListener(listener);
		findViewById(R.id.executeLuaFile).setOnClickListener(listener);
		findViewById(R.id.callAndroidApi).setOnClickListener(listener);
		findViewById(R.id.clearBtn).setOnClickListener(listener);

		mLuaState = LuaStateFactory.newLuaState();
		mLuaState.openLibs();

		EditText et = (EditText) findViewById(R.id.edit);
		et.setTextColor(Color.BLACK);
	}

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.executeLuaStatemanet:
				executeLuaStatemanet();
				break;

			case R.id.executeLuaFile:
				executeLuaFile();
				break;

			case R.id.callAndroidApi:
				callAndroidAPI();
				break;

			case R.id.clearBtn:
				displayResult1.setText("");
				displayResult2.setText("");
				mLayout.removeAllViews();
				break;
			}
		}
	};

	/**
	 * 执行lua语句
	 */
	private void executeLuaStatemanet() {
		mLuaState
				.LdoString(" varSay = 'call from android : This is string in lua script statement.'");// 下载并执行lua代码
		mLuaState.getGlobal("varSay");// 将当前变量varSay压到栈顶
		displayResult1.setText(mLuaState.toString(-1));// 从栈顶获取字符串数据，通过newstringutf返回给java层
	}

	/**
	 * 执行lua文件
	 */
	private void executeLuaFile() {
		mLuaState.LdoString(readStream(getResources().openRawResource(
				R.raw.luafile)));
		mLuaState.getField(LuaState.LUA_GLOBALSINDEX, "functionInLuaFile");// 找到functionInLuaFile函数,并放到栈顶
		mLuaState.pushString("从Java中传递的参数");// 将参数压入栈

		// functionInLuaFile函数有一个参数，一个返回结果
		int paramCount = 1;
		int resultCount = 1;
		mLuaState.call(paramCount, resultCount);
		mLuaState.setField(LuaState.LUA_GLOBALSINDEX, "resultKey");// 将结果保存到表中的resultKey字段中，并从栈中清楚数据
		mLuaState.getGlobal("resultKey");// 获取
		displayResult2.setText(mLuaState.toString(-1));// 输出
	}

	/**
	 * lua中调用android的api
	 */
	private void callAndroidAPI() {
		mLuaState.LdoString(readStream(getResources().openRawResource(
				R.raw.luafile)));
		mLuaState.getField(LuaState.LUA_GLOBALSINDEX, "callAndroidApi");// 找到functionInLuaFile函数
		mLuaState.pushJavaObject(getApplicationContext());
		mLuaState.pushJavaObject(mLayout);
		mLuaState.pushString("lua call android Textview的setText()方法, 内容是:"
				+ (++count));
		mLuaState.call(3, 0);
	}

	/**
	 * 读取lua脚本
	 * 
	 * @param is
	 * @return
	 */
	private String readStream(InputStream is) {
		try {
			ByteArrayOutputStream bo = new ByteArrayOutputStream();
			int i = is.read();
			while (i != -1) {
				bo.write(i);
				i = is.read();
			}
			return bo.toString();
		} catch (IOException e) {
			Log.e("ReadStream", "读取文件流失败");
			return "";
		}
	}
}